﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web.Mvc;
using CharacterModel;
using Framework.Model;
using MvcApplication1.Views.Portrait;
using Ninject;
using System.Linq;

namespace MvcApplication1.Controllers
{
    public class PortraitController : Controller
    {
        [Inject]
        public Repository Repository { get; set; }

        public ActionResult Create2()
        {
            return View();
        }

        public JsonResult GetImageList(int startOffset, int count)
        {
            var portraitList = Repository.FindAll<Portrait>().Reverse().Skip(startOffset).Take(count).ToList();
            var idList = portraitList.Select(portrait => portrait.Id).ToList();
            var characters = idList.Count != 0
                                 ? Repository.Find<CharacterInfo>(characterInfo => idList.Contains(characterInfo.Portrait.Id)).ToList()
                                 : new List<CharacterInfo>();

            var linqQuery = portraitList
                .SelectMany(
                    portrait => characters.Where(c => c.Portrait.Id == portrait.Id).DefaultIfEmpty(),
                    (portrait, character) => new
                                                 {
                                                     PortraitId = portrait.Id,
                                                     portrait.FaceIdentifier,
                                                     Character = character != null
                                                                     ? new
                                                                           {
                                                                               character.Id,
                                                                               character.FirstName,
                                                                               character.LastName
                                                                           }
                                                                     : null
                                                 })
                .ToList();
            return Json(linqQuery, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Save()
        {
            try
            {
                var portrait = new Portrait();
                portrait.HeadFeature = Repository.Get<Feature>(Convert.ToInt32(HttpContext.Request.Form["headId"]));
                portrait.NoseFeature = Repository.Get<Feature>(Convert.ToInt32(HttpContext.Request.Form["noseId"]));
                portrait.MouthFeature = Repository.Get<Feature>(Convert.ToInt32(HttpContext.Request.Form["mouthId"]));
                portrait.EyeFeature = Repository.Get<Feature>(Convert.ToInt32(HttpContext.Request.Form["eyesId"]));
                portrait.HeadColor = int.Parse(HttpContext.Request.Form["headColor"], NumberStyles.HexNumber);
                portrait.EyeColor = int.Parse(HttpContext.Request.Form["eyesColor"], NumberStyles.HexNumber);
                portrait.LipColor = int.Parse(HttpContext.Request.Form["lipsColor"], NumberStyles.HexNumber);
                portrait.HairColor = int.Parse(HttpContext.Request.Form["hairColor"], NumberStyles.HexNumber);
                Repository.Save(portrait);
            }
            catch (Exception e)
            {
                return Json(new {WasSuccessful = false, ErrorMessage = e.Message});
            }

            return Json(new {WasSuccessful = true});
        }

        public ActionResult Create()
        {
            return View();
        }

        public JsonResult GetColorBar(int layerTypeId)
        {
            var html = ColorBarHelper.GetColorBar((LayerTypeEnum) layerTypeId);
            return Json(new {ColorBarHtml = html}, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetFeatureIds(int featureTypeId, int offset, int maxCount)
        {
            var features = Repository.FindAll<Feature>();
            var featuresForType = features
                .Where(feature => feature.FeatureType.Id == featureTypeId)
                .ToList();

            var featureIds = featuresForType
                .Skip(offset)
                .Take(maxCount)
                .Select(feature => feature.Id)
                .ToList();

            return Json(new
                            {
                                TotalFeatures = featuresForType.Count,
                                Offset = offset,
                                MaxCount = maxCount,
                                FeatureIds = featureIds
                            }, JsonRequestBehavior.AllowGet);
        }
    }
}